from __future__ import absolute_import

from scopt.mpu import devel
from . import message

_CONTEXT_HANDLERS = ()


def set_context_handlers(mpus):
    global _CONTEXT_HANDLERS
    _CONTEXT_HANDLERS = mpus


@message(type='devel.mpu_list',
         version='0.1',
         reqd={},
         resd={})
def handle(data_packet, context):
    node = 'context'
    data_packet = devel.mpu_list_init(data_packet, 'context')
    versions = []
    for handler_type, handler in _CONTEXT_HANDLERS.items():
        if handler.VERSION not in versions:
            data_packet =\
                devel.mpu_list_append(data_packet, handler.VERSION + node,
                                      'version',
                                      'Version: ' + handler.VERSION, 'context')
            versions.append(handler.VERSION)
        data_packet =\
            devel.mpu_list_append(data_packet, handler_type, 'message',
                                  handler.TYPE, handler.VERSION + node)
    return data_packet
